import os
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from catVdog.models import IMG
from sku.recsku import predict
# Create your views here.
# 添加 index 函数，返回 index.html 页面
def index(request):
    return render(request, 'index.html')

@csrf_exempt
def uploadImg(request):
    for file in os.listdir("/home/lixuan/workspace/project/CatVsDog/media/img"):
        targetFile = os.path.join("/home/lixuan/workspace/project/CatVsDog/media/img", file)
        if os.path.isfile(targetFile):
            os.remove(targetFile)
    if request.method == 'POST':
        new_img = IMG(
            img=request.FILES.get('img'),
            name=request.FILES.get('img').name
        )
        new_img.save()
    try:
        dirs = os.listdir('/home/lixuan/workspace/project/CatVsDog/media/img')[0]
        os.system('mv /home/lixuan/workspace/project/CatVsDog/media/img/{} /home/lixuan/workspace/project/CatVsDog/media/img/test1.jpg'.format(dirs))
        predict()
    except Exception as e:
        print(e)
    return render(request, 'uploadimg.html')

def result (request):
    return render(request, 'result.html', {"data":'dog'})
